package info.mhylle.mediaplayer.playlist;


import helliker.id3.ID3v2FormatException;
import helliker.id3.MP3File;
import helliker.id3.MP3FileFilter;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class MP3PlayListGenerator {

    private List<MusicAlbum> albumList;
    private boolean level1 = true;

    public MP3PlayListGenerator() {
        albumList = new ArrayList<MusicAlbum>();
    }

    /**
     * Reads in mp3s from a directory and adds them to the this playlist.  The
     * recurse parameter should be set if you wish to grab mp3s from
     * directories as well.
     *
     * @param dir     the directory to look for mp3s in
     * @param recurse whether or not to recurse subdirectories
     * @throws java.io.IOException if the file specified is not a directory
     */
    public void loadFromDirectory(File dir, boolean recurse)
            throws IOException {

        if (dir.isDirectory()) {
            File[] files = dir.listFiles(new MP3FileFilter(true));
            MP3File mp3;
            MusicAlbum musicAlbum = new MusicAlbum();

            for (File file : files) {
                if (file.isFile()) {
                    musicAlbum.setName(dir.getName());
                    String s = file.getParent();
                    int i = s.lastIndexOf(File.separatorChar);
                    String artistPath = s.substring(0, i);
                    int i1 = artistPath.lastIndexOf(File.separator);
                    String artist = artistPath.substring(i1 + 1, artistPath.length());
                    musicAlbum.setArtist(artist);

                    try {
                        if ((file.getName().lastIndexOf(".jpg") ==
                                file.getName().length() - 4)) {
                            musicAlbum.addCoverArt(file);
                        } else {
                            mp3 = new MP3File(file);
                            musicAlbum.addTrack(mp3);
                            int releaseYear = 0;
                            try {
                                releaseYear = Integer.parseInt(mp3.getYear());
                            } catch (NumberFormatException e) {

                            } catch (ID3v2FormatException e) {

                            }
                            musicAlbum.setReleaseYear(releaseYear);
                        }
                    }
                    catch (Exception e) {
//                        System.out.println("found stuff");
                        // Do nothing.  Bad mp3, don't add.
                    }
                } else if (recurse) {
                    level1 = false;
                    loadFromDirectory(file, recurse);
                    level1 = true;
                }
            }
            if (musicAlbum.getArtist() == null && musicAlbum.getName() == null) {
            } else {
                albumList.add(musicAlbum);
            }
        } else {
            throw new IOException("Error loading playlist from a directory: "
                    + dir.getAbsolutePath() + " is not a " +
                    "directory");
        }
    }

    public List<MusicAlbum> getAlbumList() {
        return albumList;
    }
}